# -*- coding: utf-8 -*-


import unittest
import sys
import os
import time

# 添加执行环境
file_exec_path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, file_exec_path)
from framework_strategy.gopy.trader.engine import EmailEngine, MainEngine, EventEngine  # NOQA
from framework_strategy.gopy.base.api import API, Event  # NOQA
from framework_strategy.gopy.gateways.object import *  # NOQA
from framework_strategy.gopy.gateways.xt.v3.spot.xt_api import XTGateway  # NOQA
from framework_strategy.gopy.gateways.xt.v4.spot.xt4_api import XT4Gateway  # NOQA
from framework_strategy.gopy.gateways.binance.binance_api import BINANCEGateway # NOQA


def init_engine():
    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)

    xt_gateway = XT4Gateway(main_engine, event_engine, "XT4")
    xt_gateway = BINANCEGateway(main_engine, event_engine, "BINANCE")
    # event_engine.start()
    # 获取服务器时间同步获取
    api = API(event_engine)
    return event_engine, api

def init_engine_1():

    event_engine = EventEngine()
    main_engine = MainEngine(event_engine)

    xt4_gateway = XT4Gateway(main_engine, event_engine, "XT4")
    xt4_gateway.on_bind()
    return event_engine

class TestXT4SpotGateway(unittest.TestCase):

    @unittest.skip
    def test_get_servertime(self):
        
        event_engine = EventEngine()
        main_engine = MainEngine(event_engine)

        xt4_gateway = XT4Gateway(main_engine, event_engine, "XT4")
        xt4_gateway.on_bind()
        # event_engine.start()

        # 获取服务器时间同步获取
        api = API(event_engine)

        server_time_event = ServerTimeData(_source="XT4")
        print(vars(server_time_event))
        # 使用驱动调用
        nt = time.time()
        api.get_server_time(server_time_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = server_time_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_marketConfig(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)

        market_event = MarketsConfigData(_source="XT4")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_all_market_config(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_kline(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketKlineData(_source="XT4",market="btc_usdt", type="1m")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_klines(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_ticker(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketTickerData(_source="XT4",market="btc_usdt")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_ticker(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()
        pass

    @unittest.skip
    def test_get_tickers(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketTickersData(_source="XT4",market="btc_usdt")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_tickers(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_depth(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketDepthData(_source="XT4",market="btc_usdt")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_depth(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_trades(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = MarketTradesData(_source="XT4",market="btc_usdt")
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_trades(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()

    @unittest.skip
    def test_get_balance(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = AccountBalanceData(
            _source="XT4",
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a"
            )
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_balance(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()  

    @unittest.skip
    def test_get_order(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = AccountBalanceData(
            _source="XT4",
            accesskey="c15c7459-b65a-43af-8165-7ac0ae853154",
            secretkey="939a9ccc101f4df7075a1d0cdc2d3f79e88b00bd"
            )
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_order(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()  

    @unittest.skip
    def test_get_fund(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = AccountFundData(
            _source="XT4",
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a"
            )
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.get_fund(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()  

    @unittest.skip
    def test_send_order(self):

        event_engine = init_engine_1()
        # event_engine.start()
        # 获取服务器时间同步获取
        api = API(event_engine)
        market_event = AccountSendOrderData(
            _source="XT4",
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a",
            market="eth_usdt",
            price=9.9600,
            type=1,
            entrustType=0,
            number=0.01,
            _extra={
            "market":"btc_usdt",
            "price":"1000",
            "type":"1",
            "entrustType":"0",
            "number":"0.001",
            "app_name":"test", 
            "user_name":"test", 
            "uuid": 12314213,
            "ccc":"test"} # 需要额外的传参上下文
            )
        print(vars(market_event))
        # 使用驱动调用
        nt = time.time()
        api.send_order(market_event)
        # # 调用驱动来获取同步数据
        # print(time.time() - nt)

        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()  

    @unittest.skip
    def test_send_orders(self):

        event_engine = init_engine_1()
        api = API(event_engine)

        order = [
            {"symbol":"btc_usdt", "price": "19000", "quantity": "0.001", "side": "BUY", "type": "LIMIT", "timeInForce":"GTC", "bizType":"SPOT"},
            {"symbol":"btc_usdt", "price": "19001", "quantity": "0.001", "side": "BUY", "type": "LIMIT", "timeInForce":"GTC", "bizType":"SPOT"},
        ]

        market_event = AccountSendOrdersData(
            _source="XT4",
            # market="btc_usdt",
            data=order,
            accesskey="c15c7459-b65a-43af-8165-7ac0ae853154",
            secretkey="939a9ccc101f4df7075a1d0cdc2d3f79e88b00bd",
            _extra={
                "data":[
                    {
                        "market":"btc_usdt",
                        "price":"19000",
                        "type":"0",
                        "entrustType":"0",
                        "number":"0.001",
                    },
                    {
                        "market":"btc_usdt",
                        "price":"19001",
                        "type":"0",
                        "entrustType":"0",
                        "number":"0.001",
                    },
                ],
                "app_name":"test", 
                "user_name":"test", 
                "uuid": 12314213,
                "ccc":"test"
            }
        )

        nt = time.time()
        api.send_orders(market_event)
        res = market_event.get_result()

        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()
        pass

    @unittest.skip
    def test_on_event_cancel_order(self):

        event_engine = init_engine_1()
        api = API(event_engine)

        market_event = AccountCancelOrderData(
            _source="XT4",
            # market="btc_usdt",
            id="139887254047414848",
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a",
            _extra={"app_name":"test", "user_name":"test", "uuid": 12314213}
        )

        nt = time.time()
        api.cancel_order(market_event)
        res = market_event.get_result()

        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()
        pass

    @unittest.skip
    def test_on_event_cancel_orders(self):

        event_engine = init_engine_1()
        api = API(event_engine)

        data = ['139887254047414848']
        market_event = AccountCancelOrdersData(
            _source="XT4",
            # market="btc_usdt",
            data=data,
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a",
            _extra={"app_name":"test", "user_name":"test", "uuid": 12314213}
        )

        nt = time.time()
        api.cancel_orders(market_event)
        res = market_event.get_result()

        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()


    @unittest.skip
    def test_on_event_get_open_order(self):

        event_engine = init_engine_1()
        api = API(event_engine)

        market_event = AccountOpenOrderData(
            _source="XT4",
            market="btc_usdt",
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a",
        )

        nt = time.time()
        api.get_open_order(market_event)
        res = market_event.get_result()
        print("[*] res >>> ", res, time.time() - nt)
        event_engine.stop()
        pass


    # @unittest.skip
    def test_on_event_get_orders(self):

        data = '139865641251887680'
        event_engine = init_engine_1()
        api = API(event_engine)

        market_event = AccountOrdersData(
            _source="XT4",
            data=data,
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a",
        )

        nt = time.time()
        api.get_orders(market_event)
        res = market_event.get_result()

        print("[*] res >>> ", res, time.time() - nt, res.response)
        event_engine.stop()


    @unittest.skip
    def test_on_event_get_mine_orders(self):


        event_engine, api = init_engine()
        market_event = AccountMineOrdersData(
            _source="XT4",
            market="btc_usdt",
            accesskey="67783be7-b4ab-4ffc-ae3f-3268acc04c93",
            secretkey="0e16ad37592d894b75ad83aa01496a709a36475a",
        )

        nt = time.time()
        api.get_mine_trades(market_event)
        res = market_event.get_result()

        print("[*] res >>> ", res, time.time() - nt, res.response)
        event_engine.stop()    


if __name__ == "__main__":
    unittest.main()

